package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyBankSonAddBo;
import com.ruoyi.system.bo.ZsyyBankSonQueryBo;
import com.ruoyi.system.bo.ZsyyBankSonEditBo;
import com.ruoyi.system.domain.ZsyyBankSon;
import com.ruoyi.system.mapper.ZsyyBankSonMapper;
import com.ruoyi.system.vo.ZsyyBankSonVo;
import com.ruoyi.system.service.IZsyyBankSonService;
import com.ruoyi.system.mapper.ZsyyBankSonMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付子商户Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyBankSonServiceImpl extends ServicePlusImpl<ZsyyBankSonMapper, ZsyyBankSon> implements IZsyyBankSonService {
    @Autowired
    private ZsyyBankSonMapper iZsyyBankSonMapper;

    @Override
    public ZsyyBankSonVo queryById(Long id){
        return getVoById(id, ZsyyBankSonVo.class);
    }

    @Override
    public TableDataInfo<ZsyyBankSonVo> queryPageList(ZsyyBankSonQueryBo bo) {
        PagePlus<ZsyyBankSon, ZsyyBankSonVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyBankSonVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyBankSonVo> queryList(ZsyyBankSonQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyBankSonVo.class);
    }

    private LambdaQueryWrapper<ZsyyBankSon> buildQueryWrapper(ZsyyBankSonQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyBankSon> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAboutId() != null, ZsyyBankSon::getAboutId, bo.getAboutId());
        lqw.eq(bo.getAboutType() != null, ZsyyBankSon::getAboutType, bo.getAboutType());
        lqw.eq(StrUtil.isNotBlank(bo.getMid()), ZsyyBankSon::getMid, bo.getMid());
        lqw.eq(StrUtil.isNotBlank(bo.getTid()), ZsyyBankSon::getTid, bo.getTid());
        lqw.eq(StrUtil.isNotBlank(bo.getKey()), ZsyyBankSon::getKey, bo.getKey());
        lqw.eq(bo.getFenRatio() != null, ZsyyBankSon::getFenRatio, bo.getFenRatio());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyBankSonAddBo bo) {
        ZsyyBankSon add = BeanUtil.toBean(bo, ZsyyBankSon.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyBankSonEditBo bo) {
        ZsyyBankSon update = BeanUtil.toBean(bo, ZsyyBankSon.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyBankSon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
